/**
 * Project Name:fastPlat
 * File Name:MemberFavoritesController.java
 * Package Name:com.fast.user.web.controller.member
 * Date:2017年05月27日 17时59分49秒
 * Copyright (c) 2017, lsflu@126.com All Rights Reserved.
 *
*/

package com.fast.user.web.controller.member;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.fast.base.db.page.PageView;
import com.fast.base.db.sql.AbstractSqlGenerator;
import com.fast.base.model.AnoSysLog;
import com.fast.base.util.LogUtils;
import com.fast.base.util.StringUtils;
import com.fast.base.util.web.ResponseData;
import com.fast.base.util.web.ResponseEnum;
import com.fast.base.web.BaseController;
import com.fast.common.userCache.UserRequestUtils;
import com.fast.user.model.member.MemberFavorites;
import com.fast.user.service.member.MemberFavoritesService;
import com.fast.user.web.form.web.MemberFavoritesForm;

/**
 * ClassName:MemberFavoritesController <br/>
 * 用途: 用户收藏记录 web控制类 <br/>
 * 开发人员：lushuifa  http://lushuifa.iteye.com/
 * 邮箱:lsflu@126.com
 * @version  1.0
 * @since    JDK 1.6
 * @see
 * Date:     2017年05月27日 17时59分49秒 <br/> 	 
 */
@Controller
@RequestMapping("/memberFavorites")
public class MemberFavoritesController extends BaseController{
	
	
	@Resource(name="memberFavoritesService")
	private MemberFavoritesService memberFavoritesService;

	/**
	 * 编辑用户收藏记录信息
	 * @param imageUrl
	 * @return
	 */
	@RequestMapping("/editMemberFavorites.do")
	@ResponseBody
	@AnoSysLog(actionName="编辑用户收藏记录信息")
	public ResponseData editMemberFavorites(){
		ResponseData responseData = new ResponseData();
		MemberFavorites memberFavorites = null;
		try {
			int id = webUtil.getInt("id");
			if(id>0){
				memberFavorites = this.memberFavoritesService.getById(MemberFavorites.class,id);
				memberFavorites = (MemberFavorites) webUtil.bindDataToBean(memberFavorites);
			}else{
				memberFavorites = new MemberFavorites();
				memberFavorites = (MemberFavorites) webUtil.bindDataToBean(memberFavorites);
				UserRequestUtils.bindCreaterInfo(memberFavorites);
			}
			UserRequestUtils.bindEditorInfo(memberFavorites);
			if(this.memberFavoritesService.merge(memberFavoritesService)!=null){
				responseData.buildResult(ResponseEnum.EDIT_SUCCESS);
			}else{
				responseData.buildResult(ResponseEnum.EDIT_FAILUE);
			}
		} catch (Exception e) {
			e.printStackTrace();
			responseData.failue();
		}
		return responseData;
	}
	
	/**
	 * 删除用户收藏记录信息到回收站
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/delMemberFavoritesByIdForHS.do")
	@AnoSysLog(actionName="删除用户收藏记录到回收站")
	public ResponseData delMemberFavoritesByIdForHS(int id) {
		LogUtils.info("delMemberFavoritesByIdForHS id=:"+id);
		ResponseData responseData = new ResponseData();
		try {
			String sql = "update "+AbstractSqlGenerator.getTableName(MemberFavorites.class)+" set delete_Flag=1 where id="+id;
			boolean actionFlag = memberFavoritesService.modifyRow(sql)>0;
			responseData.setCode(actionFlag?"00":"01");
			if(actionFlag){
				responseData.buildResult(ResponseEnum.DELETE_SUCCESS);
			}else{
				responseData.buildResult(ResponseEnum.DELETE_FAILUE);
			}
		} catch (Exception e) {
			e.printStackTrace();
			responseData.failue();
		}
		LogUtils.info("delMemberFavoritesByIdForHS responseData=:"+responseData.toString());
		return responseData;
	}

	/**
	 * 删除用户收藏记录信息
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/delMemberFavoritesById.do")
	@AnoSysLog(actionName="彻底删除用户收藏记录")
	public ResponseData delMemberFavoritesById(int id) {
		LogUtils.info("delMemberFavoritesById id=:"+id);
		ResponseData responseData = new ResponseData();
		try {
			boolean actionFlag = memberFavoritesService.deleteById(MemberFavorites.class, id);
			responseData.setCode(actionFlag?"00":"01");
			if(actionFlag){
				responseData.buildResult(ResponseEnum.DELETE_SUCCESS);
			}else{
				responseData.buildResult(ResponseEnum.DELETE_FAILUE);
			}
		} catch (Exception e) {
			e.printStackTrace();
			responseData.failue();
		}
		LogUtils.info("delMemberFavoritesById responseData=:"+responseData.toString());
		return responseData;
	}
	
	/**
	 * 删除多个用户收藏记录信息
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/batchDelMemberFavorites.do")
	@AnoSysLog(actionName="批量删除用户收藏记录评论")
	public ResponseData batchDelMemberFavorites(String ids){
		ResponseData responseData = new ResponseData();
		try {
			int actionCount = memberFavoritesService.deleteByIds(MemberFavorites.class, ids);
			if(actionCount>0){
				responseData.buildResult(ResponseEnum.DELETE_SUCCESS);
			}else{
				responseData.buildResult(ResponseEnum.DELETE_FAILUE);
			}
		} catch (Exception e) {
			e.printStackTrace();
			responseData.failue();
		}
		return responseData;
	}
	
	/**
	 * 分页得到用户收藏记录列表
	 * @return PageView<MemberFavorites>
	 * @throws IOException 
	 */
	@ResponseBody
	@RequestMapping("/getMemberFavoritesList.do")
	public Object getMemberFavoritesList(MemberFavoritesForm memberFavoritesForm){
		LogUtils.info("getMemberFavoritesList param:"+memberFavoritesForm.toString());
		Map<String,Object> gridMap = new HashMap<String,Object>();
		PageView<MemberFavorites> pageView = null;
		try {
			pageView = this.memberFavoritesService.getMemberFavoritesList(memberFavoritesForm);
			gridMap.put("total", pageView.getTotalRecord());
			gridMap.put("rows", pageView.getRecords());
		} catch (Exception e) {
			LogUtils.error("getMemberFavoritesList err:",e);
		}
		LogUtils.info("getMemberFavoritesList result:"+pageView.toString());
 		return gridMap;
	}
	
	/**
	 * 查询用户收藏记录详情
	 * @param id
	 * @return
	 */
	@RequestMapping("/getMemberFavoritesDetail.do")
	public ModelAndView getMemberFavoritesDetail(int id,String actionType) {
		ModelAndView mv = new ModelAndView();
		MemberFavorites bean = memberFavoritesService.queryForBeanById(MemberFavorites.class, id);
		try {
			mv.addObject("memberFavorites",bean);
		} catch (Exception e) {
			e.printStackTrace();
		}
		if(StringUtils.equals(actionType,"update")){
			mv.setViewName("/adminMain/memberFavorites/memberFavorites-add.jsp");
		}else{
			mv.setViewName("/adminMain/memberFavorites/memberFavorites-detail.jsp");
		}
		return mv;
	}
}

